package com.sunday.volunteer.ui.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.ToastUtils;
import com.sunday.common.widgets.banner.ConvenientBanner;
import com.sunday.member.base.BaseActivity;
import com.sunday.volunteer.BaseApp;
import com.sunday.volunteer.R;
import com.sunday.volunteer.entity.Activity;
import com.sunday.volunteer.http.HttpClient;
import com.sunday.volunteer.utils.BaseUtil;
import com.sunday.volunteer.utils.StatusBarUtil;
import com.sunday.volunteer.utils.ViewPagerUtils;


import java.util.Arrays;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by admin on 2017/1/11.
 */

public class ActivityDetialActivity extends BaseActivity {

    @Bind(R.id.viewpager)
    ConvenientBanner viewpager;
    @Bind(R.id.name)
    TextView name;
    @Bind(R.id.time)
    TextView time;
    @Bind(R.id.address)
    TextView address;
    @Bind(R.id.count)
    TextView count;
    @Bind(R.id.group_name)
    TextView group_name;
    @Bind(R.id.score)
    TextView score;
    @Bind(R.id.logo)
    ImageView logo;
    @Bind(R.id.ratingStar)
    RatingBar ratingStar;
    @Bind(R.id.desc)
    TextView desc;
    @Bind(R.id.join_num)
    TextView join_num;
    @Bind(R.id.recycler_view)
    RecyclerView recyclerView;
    @Bind(R.id.btn)
    TextView btn;


    private String imgPath;
    private int status = -1;
    private Activity activity;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_activity_detail);
        ButterKnife.bind(this);
        StatusBarUtil.setTransparentForImageView(this , null);

        getData();
    }

    private void getData() {
        Call<ResultDO<Activity>> call = HttpClient.getHttpAdapter().getActivityDetail(BaseApp.getInstance().getMemberId(), getIntent().getLongExtra(BaseUtil.ID, -1));
        call.enqueue(new Callback<ResultDO<Activity>>() {
            @Override
            public void onResponse(Call<ResultDO<Activity>> call, Response<ResultDO<Activity>> response) {
                if (response.body() == null || isFinish) {
                    return;
                }
                if (response.body().getCode() == 0 && response.body().getResult() != null) {
                    activity = response.body().getResult();
                    imgPath = activity.getEwm();
                    status = activity.getStatus();
                    setData(activity);
                } else {
                    ToastUtils.showToast(mContext, response.body().getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO<Activity>> call, Throwable t) {

            }
        });

    }

    @OnClick(R.id.to_big_img)
    void tobigImg(){
        if(imgPath == null){
            imgPath = "http";
        }
        BaseUtil.openBigImage(mContext , imgPath);
    }

    @OnClick(R.id.btn)
    void Btn(){
        if(status == 3 || status == 6){
            Baoming();
        }
    }
    @OnClick(R.id.to_group)
    void toGroup(){
        BaseUtil.openGroupDetialActivity(mContext , activity.getGroupId());
    }
    private void setData(final Activity item) {
        switch (status){
            case 3:
                btn.setText("我要报名");
                break;
            case 6:
                btn.setText("取消报名");
                break;
            case 4:
                btn.setText("活动进行中");
                btn.setBackgroundColor(getResources().getColor(R.color.item_pressed));
                break;
            case 5:
                btn.setText("活动已结束");
                btn.setBackgroundColor(getResources().getColor(R.color.item_pressed));
                break;
            case 7:
                btn.setText("活动进行中,已签到");
                btn.setBackgroundColor(getResources().getColor(R.color.item_pressed));
                break;
            case 8:
                btn.setText("活动已结束,已签退");
                btn.setBackgroundColor(getResources().getColor(R.color.item_pressed));
                break;
        }

        new ViewPagerUtils(mContext , viewpager , Arrays.asList(item.getRollImages() ));

        name.setText(item.getName());
        time.setText(item.getTime());
        address.setText(item.getAddress());
        count.setText(String.format("%d%s", item.getJoinNumber(), "人"));
        group_name.setText(item.getGroupName());
        score.setText(String.format("%.0f", item.getSocre()));
        join_num.setText(String.format("%d%s%d", item.getJoinNumber(), "/", item.getLimitNumber()));
        desc.setText(item.getDesc());
        Picasso.with(mContext).load(TextUtils.isEmpty(item.getGroupImage())?"http": item.getGroupImage()).resize(80, 80).centerCrop().placeholder(R.drawable.default_error).into(logo);

        ratingStar.setRating(1);

        recyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        recyclerView.setAdapter(new RecyclerView.Adapter() {

            @Override
            public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(mContext).inflate(R.layout.item_activity_detail, null);
                return new ViewHolder(view);
            }

            @Override
            public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
                ViewHolder viewHolder = (ViewHolder) holder;
                Picasso.with(mContext).load(item.getMembers()[position]).resize(80, 80).centerCrop().placeholder(R.drawable.default_portrait).into(viewHolder.imageView);
            }

            @Override
            public int getItemCount() {
                return item.getMembers().length;
            }
        });


    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.image_view)
        ImageView imageView;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    private void Baoming(){
        Call<ResultDO> call = null;
        if(status == 3){
            call = HttpClient.getHttpAdapter().baoming(BaseApp.getInstance().getMemberId() , getIntent().getLongExtra(BaseUtil.ID, -1));
        }else if(status == 6){
           call = HttpClient.getHttpAdapter().quxiaobaoming(BaseApp.getInstance().getMemberId() , getIntent().getLongExtra(BaseUtil.ID, -1));
        }

        call.enqueue(new Callback<ResultDO>() {
            @Override
            public void onResponse(Call<ResultDO> call, Response<ResultDO> response) {
                if (response.body() == null || isFinish) {
                    return;
                }
                if (response.body().getCode() == 0 ) {
                    if(status == 3){
                        ToastUtils.showToast(mContext, "报名成功");
                    }else if(status == 6){
                        ToastUtils.showToast(mContext, "取消报名成功");
                    }
                    getData();
                } else {
                    ToastUtils.showToast(mContext, response.body().getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO> call, Throwable t) {

            }
        });

    }


}